<?php
    require_once('../../global.php');
    $prefix = BASE_DATA_PATH."/vendors/weixin";
//    require_once($prefix.'/WxPayPubHelper/WxPayPubHelper.php');
    require_once($prefix.'/lib/WxPay.Data.php');
    include_once($prefix."/log_.php");
// ----------------------------------------------------------------------
//测试数据
//    $xml="<xml><appid><![CDATA[wx8f4b141c9596bdca]]></appid>
//    <attach><![CDATA[test]]></attach>
//    <bank_type><![CDATA[CFT]]></bank_type>
//    <cash_fee><![CDATA[1]]></cash_fee>
//    <fee_type><![CDATA[CNY]]></fee_type>
//    <is_subscribe><![CDATA[Y]]></is_subscribe>
//    <mch_id><![CDATA[1266784801]]></mch_id>
//    <nonce_str><![CDATA[wvs0ho5e0sf1ugo2la6c4189idl6gdby]]></nonce_str>
//    <openid><![CDATA[oShzNjt3m288v65Amb4Eoqj9l71I]]></openid>
//    <out_trade_no><![CDATA[2016122616405565434487]]></out_trade_no>
//    <result_code><![CDATA[SUCCESS]]></result_code>
//    <return_code><![CDATA[SUCCESS]]></return_code>
//    <sign><![CDATA[8F60504707170B551A9F1C98B0F8CAD6]]></sign>
//    <time_end><![CDATA[20161226184721]]></time_end>
//    <total_fee>1</total_fee>
//    <trade_type><![CDATA[JSAPI]]></trade_type>
//    <transaction_id><![CDATA[4005892001201612264078332768]]></transaction_id>
//</xml>";
    $notify = new WxPayResults();
    $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
    $data = $notify->FromXml($xml);
// ----------------------------------------------------------------------
// 记录返回通知日志
    $time = date('Y-m-d', time());
    $log_ = new Log_();
    $log_name = BASE_DATA_PATH."/log/wx/" . $time . ".log";
    if(!file_exists($log_name)){
        $fp=fopen("$log_name", "w+");
    }
    $log_->log_result($log_name, "【接收到的notify通知】:\n".$xml."\n");

// ----------------------------------------------------------------------
    if($notify->CheckSign() == true) {
        if ($data["return_code"] == "FAIL") {
            $log_->log_result($log_name, "【通信出错】:\n".$xml."\n");
        }
        elseif($data["result_code"] == "FAIL"){
            $log_->log_result($log_name, "【业务出错】:\n".$xml."\n");
        }
        else{
            $log_->log_result($log_name, "【支付成功】:\n".$xml."\n");
            // 参数设定
            $_POST['trade_status'] = $data["return_code"];
            $_POST['out_trade_no'] = $data["out_trade_no"];
            $_POST['trade_no'] 	   = $data["transaction_id"];   //微信支付单号
            $_POST['attach']       = $data['attach'];
            $_POST['total_fee']    = $data['total_fee'];//订单总金额

            // --- 启用系统框架
            $_GET['act'] = 'store_notify';
            $_GET['op'] = 'wxNotify';
            require_once(dirname(__FILE__).'/../index.php');
        }

    }
?>